/*
 * @lc app=leetcode.cn id=89 lang=javascript
 *
 * [89] 格雷编码
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number[]}
 */
var grayCode = function (n) {
  let db = [];
  db[0] = [0];
  db[1] = [0, 1];
  db[2] = [0, 1, 3, 2];
  for (let i = 3; i <= n; i++) {
    db[i] = [...db[i - 1]];
    for (let j = db[i - 1].length-1; j >= 0; j--) {
        db[i].push(db[i-1][j]+(1<<i-1))
    }
  }
  return db[n];
};
// @lc code=end
